package com.lanou3g.musicenjoy.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou3g.musicenjoy.adapter.MusicRecycleViewAdapter;
import com.lanou3g.musicenjoy.entity.Music;
import com.lanou3g.musicenjoy.adapter.MusicAdapter;
import com.lanou3g.musicenjoy.R;
import com.lanou3g.musicenjoy.utils.ListViewForScrollView;
import com.lanou3g.musicenjoy.volley.VolleySingleton;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by dllo on 15/10/20.
 */
public class MusicFragment extends Fragment {

    ImageView inforImageView;
    @Bind(R.id.musicFragListView)
    RecyclerView musicFragListView;
    private RequestQueue requestQueue;
    private List<Music.SonglistEntity> datas;
   // private MusicAdapter adapter;
   private RecyclerView.Adapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_music, null);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        requestQueue= VolleySingleton.getInstance(getActivity()).getRequestQueue();
        musicFragListView = (RecyclerView) getActivity().findViewById(R.id.musicFragListView);


    }


    @Override
    public void onResume() {
        super.onResume();
        musicFragListView.setLayoutManager(null);
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        musicFragListView.setLayoutManager(manager);
        Bundle bundle = getArguments();
        String a = bundle.getString("aa");
        String url="http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.0&method=baidu.ting.artist.getSongList&" +
                "format=json&order=2&tinguid="+a+"&artistid="+a+"&offset=0&limits=50";
        Log.i("fjdfjdf",url);
        StringRequest request=new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (response!=null){
                    Gson gson=new Gson();
                    Music result=gson.fromJson(response,Music.class);
                    if (result!=null){
                        datas=result.getSonglist();
                        //adapter.initData(datas);
                        adapter=new MusicRecycleViewAdapter(getActivity(),datas);
                        musicFragListView.setAdapter(adapter);
                        Log.i("fjaklfa",datas+"");
                    }
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        requestQueue.add(request);


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
